using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using Domain;

namespace Persistent
{
    public interface IPlayerRepository : IDisposable
    {
        IQueryable<Player> All { get; }
        Player Find(int id);
        void InsertOrUpdate(Player player);
        void Delete(int id);
        void Save();
    }

    public class PlayerRepository : BaseRepository, IPlayerRepository
    {
        public IQueryable<Player> All
        {
            get { return context.Players; }
        }

        public Player Find(int id)
        {
            return context.Players.Find(id);
        }

        public void InsertOrUpdate(Player player)
        {
            if (player.PlayerId == default(int)) 
            {
                context.Players.Add(player);
            } 
            else 
            {
                context.Entry(player).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var player = context.Players.Find(id);
            context.Players.Remove(player);
        }
        
    }

}